# -*-coding:gbk-*-
# ProjectName: smart-meter-iot-test-console
# FileName: reportTask.py
# @DateTime :2023/4/7 23:46
# @Author :
# @Description:��ʱ�ϱ�����
import time
from common.util.mySqlUtil.mySqlPool import select_sql
from common.util.timeUtil.timeUtil import get_now_time
from core.mqtt.connect.mqtt import *
from core.mqtt.libProject.reportData.reportData import *

read_json = JsonUtil()
config = ConfigUtil()
deviceName = config.get_mqtt_config("deviceName")
productKey = config.get_productkey()
client = mqtt_connect(deviceName)
logger = LoggerUtil()


def log_record(status: int = 1):
    if status == 1:
        startContent = "��ʼ"
    else:
        startContent = "����"
    print(f"�ϱ�{startContent}ʱ��:{get_now_time(1)}")
    logger.info(f"�ϱ�{startContent}ʱ��:{get_now_time(1)}")
    logger.debug(f"�ϱ�{startContent}ʱ��:{get_now_time(1)}")


# ���ڻ������ϱ�
def report_indoor_init_info() -> None:
    """
    :return:
    """
    try:
        log_record()
        sql = read_json.read_sql("indoorUnitInfo").format(deviceName)
        dbDataList = select_sql(sql)
        # print(dbDataList)
        count = 1
        for i in dbDataList:
            if count % 15 == 0:
                print(f"����:{count}")
                logger.debug(f"����:{count}")
                sleep(0.5)
            # ������ϵ�ϱ�
            reporData = indoor_unit_data_body(i[1], i[2])
            topic = f"/sys/{config.get_productkey()}/{deviceName}/thing/event/property/post"
            on_publish(client, topic, str(reporData), 0)
            logger.info(f"�ϱ�����:\n{str(reporData)}")
            print(f"�ϱ�����:\n{str(reporData)}")
            count += 1
        log_record(2)
    except Exception as e:
        raise e


# ������ϵ�ϱ�
def report_association() -> None:
    """
    :return:
    """
    try:
        log_record()
        reporData = association_data_body(deviceName)
        topic = f"/{config.get_productkey()}/{deviceName}/user/update"
        on_publish(client, topic, str(reporData), 0)
        logger.info(f"�ϱ�����:\n{str(reporData)}")
        print(f"�ϱ�����:  {reporData}")
        log_record(2)
    except Exception as e:
        raise e


# ���ڻ����ػ�ʱ���ϱ�
def report_switchOnoff() -> None:
    """
    :return:
    """
    try:
        log_record()
        addr = "V10230800200node168530682"
        reporData = indoorunit_switchonoff_data_body(addr)
        topic = f"/sys/{config.get_productkey()}/{deviceName}/thing/event/switch:onoff/post"
        print(topic)
        on_publish(client, topic, str(reporData), 0)
        logger.info(f"�ϱ�����:\n{str(reporData)}")
        print(f"�ϱ�����:  {reporData}")
        log_record(2)
    except Exception as e:
        raise e


# ��վ(����)��Ϣ�ϱ�
def report_collector() -> None:
    """
    :return:
    """
    try:
        log_record()
        reporData = collector_data_body(deviceName)
        topic = f"/sys/{config.get_productkey()}/{deviceName}/thing/event/property/post"
        on_publish(client, topic, str(reporData), 0)
        logger.info(f"�ϱ�����:\n{str(reporData)}")
        print(f"�ϱ�����:  {reporData}")
        log_record(2)
    except Exception as e:
        raise e


# �����ϱ�
def report_error() -> None:
    """
    :return:
    """
    try:
        log_record()
        reporData = error_data_body(deviceName)
        topic = f"/sys/{config.get_productkey()}/{deviceName}/thing/event/abnormal:error/post"
        on_publish(client, topic, str(reporData), 0)
        logger.info(f"�ϱ�����:\n{str(reporData)}")
        print(f"�ϱ�����:  {reporData}")
        log_record(2)
    except Exception as e:
        raise e


def report_task() -> None:
    """
    :��ʱ�ϱ���ÿ��30���ϱ�һ��:
    """
    while True:
        report_indoor_init_info()
        sleep(3600)


if __name__ == '__main__':
    # ��վ(����)��Ϣ�ϱ�
    # report_collector()
    # �����ϱ�
    # report_error()
    # sleep(1)
    # ������ϵ�ϱ�
    report_association()
    # ���ڻ������ϱ�
    # report_indoor_init_info()
    # ���ڻ������ϱ�
    # report_switchOnoff()
